﻿using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;

namespace ServerTest
{
    public class LogHelp
    {
        static string LogPath = AppDomain.CurrentDomain.BaseDirectory + "ServerTest.exe.config";

        public static void ConfigLog4Net()
        {
            var logCfg = new FileInfo(LogPath);
            XmlConfigurator.Configure(logCfg);
        }


        public static void AddText(Action<string> textBoxBase)
        {
            //读取配置文件的信息  
            //log1 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //设置textbox打印日志  
            var logPattern = "%d{yyyy-MM-dd HH:mm:ss} --%-5p-- %m%n";
            var textBox_logAppender = new TextBoxBaseAppender()
            {
                Action = textBoxBase,//注释后 就只有文件log  
                Layout = new PatternLayout(logPattern)
            };
            //相当于root标签下的   <appender-ref ref="LogFile" />  
            log4net.Config.BasicConfigurator.Configure(textBox_logAppender);

        }

        public static void AddText(TextBoxBase textBoxBase)
        {
            //读取配置文件的信息  
            //log1 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //设置textbox打印日志  
            var logPattern = "%date [%thread](%.18file:%5line) %5level %logger - %message%newline";
            var textBox_logAppender = new TextBoxBaseAppender()
            {
                TextBox = textBoxBase,//注释后 就只有文件log  
                Layout = new PatternLayout(logPattern)
            };
            //相当于root标签下的   <appender-ref ref="LogFile" />  
            log4net.Config.BasicConfigurator.Configure(textBox_logAppender);

        }
    }


    /// <summary>  
    /// Usage:  
    ///     log4net.Config.BasicConfigurator.Configure();  
    ///     var logPattern = "%date [%thread] %-5level %logger !%M - %message%newline";  
    ///     var logAppender = new TextBoxBaseAppender()  
    ///     {  
    ///         TextBox = this.textBox2,  
    ///         Layout = new PatternLayout(logPattern)  
    ///     };  
    ///       
    ///     ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepository()).Root.AddAppender(logAppender);  
    /// </summary>  
    public class TextBoxBaseAppender : AppenderSkeleton
    {
        public TextBoxBase TextBox { get; set; }
        public Action<string> Action;

        public TextBoxBaseAppender()
        {
        }

        protected override void Append(LoggingEvent loggingEvent)
        {
            if (this.Action == null)
            {
                return;
            }

            //if (!this.TextBox.IsHandleCreated)
            //{
            //    return;
            //}

            //if (this.TextBox.IsDisposed)
            //{
            //    return;
            //}

            var patternLayout = this.Layout as PatternLayout;

            var str = string.Empty;
            if (patternLayout != null)
            {
                str = patternLayout.Format(loggingEvent);

                if (loggingEvent.ExceptionObject != null)
                {
                    str += loggingEvent.ExceptionObject.ToString() + Environment.NewLine;
                }
            }
            else
            {
                str = loggingEvent.LoggerName + "-" + loggingEvent.RenderedMessage + Environment.NewLine;
            }

            printf(str);

        }

        private void printf(string str)
        {
            ////若是超过10行 则清楚  
            //if (TextBox.Lines.Length > 50)
            //{
            //    TextBox.Clear();
            //}
            //this.TextBox.AppendText(str);
            this.Action(str);
        }
    }
}
